package com.sike.netty.jinjie.channelPipeline.event;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

public class ExceptionHandler extends ChannelDuplexHandler {

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // do something
        cause.printStackTrace();

        ctx.close();
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        ctx.write(msg, promise.addListener(future -> {
            if (future.isSuccess()) {
                Throwable cause = future.cause();
                cause.printStackTrace();

                // do something
            }
        }));
    }
}
